// pages/SCM/store/goodsAdd/goodsAdd.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  data: {
    id: '',
    goodsList: [],
    goodsStatIds: [],
    uindex: 0,
    goodsSend: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({ id: options.id });
      this.goodsSendItem();
    }
    this.getCurUserHasRoles();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    that.toast = that.selectComponent("#toast")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    //
    //添加物品
    if (this.data.addGoodsFlag) {
      if (this.data.goodsStatIds.indexOf(this.data.selectGoods.goodsStatId) > -1) {
        wx.showToast({
          title: '该物品已存在',
          image: '/images/gantan.png'
        })
        return;
      }
      this.data.goodsList.push(this.data.selectGoods);
      this.data.goodsStatIds.push(this.data.selectGoods.goodsStatId);

      this.setData({
        goodsList: this.data.goodsList
      });
      this.data.addGoodsFlag = false;
      this.data.selectGoods = {};
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  getCurUserHasRoles: function () {
    let that = this;
    lbbService.getCurUserHasRoles({}, function (res) {

      
      that.setData({ curUser: res.data });
    });
  },
  
  goodsSendItem: function () {
    let that = this;
    let goodsList = [];
    let goodsStatIds = [];
    lbbService.goodsSendItem({ id: this.data.id }, function (res) {
      res.data.goodsAccessDetails.forEach(item => {
        let selectGoods = {};
        selectGoods.goodsId = item.goods.id;
        selectGoods.goodsName = item.goods.name;
        //selectGoods.goodsUnit = item.goods.goodsUnit;
        selectGoods.goodsStatId = item.goodsId;
        //selectGoods.goodsNum = item.goodsNum;
        selectGoods.addNum = item.goodsNum;
        goodsList.push(selectGoods);
        goodsStatIds.push(item.goodsId);
      });
      that.setData({
        goodsSend: res.data,
        goodsList: goodsList,
        goodsStatIds: goodsStatIds,
      });
    });
  },

  goodsSendConfirm: function () {
    let that = this
    lbbService.goodsSendConfirm({ id: that.data.id }, function (res) {
      wx.showToast({
        title: '提交成功',
      })
      let goodsSend = that.data.goodsSend;
      goodsSend.sendStatus=1;
      that.setData({
        goodsSend: goodsSend
      });
    })
  },
  
  previewImage: function (e) {
    var urls = e.target.dataset.src;
    wx.previewImage({
      urls: urls // 需要预览的图片http链接列表
    })
  },




})